package com.ouli.crm.workbench.servce.impl;

import com.ouli.crm.settings.dao.UserDao;
import com.ouli.crm.settings.domain.User;
import com.ouli.crm.utils.SqlSessionUtil;
import com.ouli.crm.vo.PaginationVO;
import com.ouli.crm.workbench.dao.ActivityDao;
import com.ouli.crm.workbench.dao.ActivityRemarkDao;
import com.ouli.crm.workbench.domain.Activity;
import com.ouli.crm.workbench.servce.ActivityService;

import java.util.*;

public class ActivityServiceImpl implements ActivityService {

    private ActivityDao activityDao = SqlSessionUtil.getSqlSession().getMapper(ActivityDao.class);
    private ActivityRemarkDao activityRemarkDao = SqlSessionUtil.getSqlSession().getMapper(ActivityRemarkDao.class);
    private UserDao userDao = SqlSessionUtil.getSqlSession().getMapper(UserDao.class);

    public boolean save(Activity activity) {
        boolean flag = true;
        int count = activityDao.save(activity);
        if (count != 1) {

            flag=false;
        }
        return flag;
    }

    public PaginationVO<Activity> pageList(Map<String, Object> map) {
        int total = activityDao.getTotalByCondition(map);

        List<Activity> activity = activityDao.getActivityListByCondition(map);

        PaginationVO<Activity> vo = new PaginationVO<Activity>();
        vo.setDataList(activity);
        vo.setTotal(total);
        return vo;
    }

    public boolean delete(String[] ids) {
        boolean flag = true;

        //查询出需要删除的备注的数量

        int count1 = activityRemarkDao.getCountByAids(ids);

        //删除备注，返回收到影响的条数（实际删除的数量）

        int count2 = activityRemarkDao.deleteByAids(ids);

        if (count1!=count2) {
            flag = false;
        }

        //删除市场活动
        int count3 = activityDao.delete(ids);
        if (count3!=ids.length) {
            flag = false;
        }
        return flag;
    }

    public Map<String, Object> getUserAndWorkbench(String id) {
        List<User> ulist = userDao.getUserList();
        Activity activity = activityDao.getActivityById(id);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("uList",ulist);
        map.put("a",activity);
        return map;
    }

    public boolean updateActivity(Activity activity) {
        boolean flag = true;
        int count = activityDao.updateActivity(activity);
        if (count!=1){

            flag = false;

        }

        return flag;
    }

    public Activity detail(String id) {

        Activity activity = activityDao.detail(id);

        return activity;
    }
}
